package com.huaxin.hxmodulemqtt.mapping;

import java.util.HashMap;
import java.util.Map;

public class DeviceMetricFieldMapping {
    private static final Map<String, String> METRIC_TO_FIELD = new HashMap<>();

    static {
        // ========== 10KV电力监测仪表==========
        put("power_meter_10kv", "10004", "voltageAKv");
        put("power_meter_10kv", "10005", "voltageBKv");
        put("power_meter_10kv", "10006", "voltageCKv");
        put("power_meter_10kv", "10021", "voltageAbKv");
        put("power_meter_10kv", "10022", "voltageBcKv");
        put("power_meter_10kv", "10023", "voltageCaKv");
        put("power_meter_10kv", "10001", "currentAA");
        put("power_meter_10kv", "10002", "currentBA");
        put("power_meter_10kv", "10003", "currentCA");
        put("power_meter_10kv", "10018", "powerFactorTotal");
        put("power_meter_10kv", "10017", "activePowerTotalKw");
        put("power_meter_10kv", "10019", "reactivePowerTotalKvar");
        put("power_meter_10kv", "10057", "apparentPowerTotalKva");
        put("power_meter_10kv", "10020", "frequencyHz");
        put("power_meter_10kv", "10031", "energyActiveForwardKwh");
        put("power_meter_10kv", "10033", "energyActiveReverseKwh");
        put("power_meter_10kv", "10032", "energyReactiveForwardKwh");
        put("power_meter_10kv", "10034", "energyReactiveReverseKwh");

        // ========== 0.4KV电力监测仪表 ==========
        put("power_meter_04kv", "10004", "voltageAKv");
        put("power_meter_04kv", "10005", "voltageBKv");
        put("power_meter_04kv", "10006", "voltageCKv");
        put("power_meter_04kv", "10021", "voltageAbKv");
        put("power_meter_04kv", "10022", "voltageBcKv");
        put("power_meter_04kv", "10023", "voltageCaKv");
        put("power_meter_04kv", "10001", "currentAA");
        put("power_meter_04kv", "10002", "currentBA");
        put("power_meter_04kv", "10003", "currentCA");
        put("power_meter_04kv", "10018", "powerFactorTotal");
        put("power_meter_04kv", "10017", "activePowerTotalKw");
        put("power_meter_04kv", "10019", "reactivePowerTotalKvar");
        put("power_meter_04kv", "10057", "apparentPowerTotalKva");
        put("power_meter_04kv", "10020", "frequencyHz");
        put("power_meter_04kv", "10031", "energyActiveForwardKwh");
        put("power_meter_04kv", "10033", "energyActiveReverseKwh");
        put("power_meter_04kv", "10032", "energyReactiveForwardKwh");
        put("power_meter_04kv", "10034", "energyReactiveReverseKwh");

        // ========== 微机保护 ==========
        put("microprocessor_relay", "10004", "voltageAKv");
        put("microprocessor_relay", "10005", "voltageBKv");
        put("microprocessor_relay", "10006", "voltageCKv");
        put("microprocessor_relay", "10021", "voltageAbKv");
        put("microprocessor_relay", "10022", "voltageBcKv");
        put("microprocessor_relay", "10023", "voltageCaKv");
        put("microprocessor_relay", "10001", "currentAA");
        put("microprocessor_relay", "10002", "currentBA");
        put("microprocessor_relay", "10003", "currentCA");
        put("microprocessor_relay", "10018", "powerFactorTotal");
        put("microprocessor_relay", "10017", "activePowerTotalKw");
        put("microprocessor_relay", "10019", "reactivePowerTotalKvar");
        put("microprocessor_relay", "10057", "apparentPowerTotalKva");
        put("microprocessor_relay", "10020", "frequencyHz");
        put("microprocessor_relay", "10031", "energyActiveForwardKwh");
        put("microprocessor_relay", "10033", "energyActiveReverseKwh");
        put("microprocessor_relay", "10032", "energyReactiveForwardKwh");
        put("microprocessor_relay", "10034", "energyReactiveReverseKwh");
        put("microprocessor_relay", "11001", "breaker1Status");
        put("microprocessor_relay", "11002", "breaker2Status");
        put("microprocessor_relay", "11003", "breaker3Status");

        // ========== 配电房环境检测 ==========
        put("env_monitor_room", "10044", "temperatureRoom");
        put("env_monitor_room", "10045", "humidityRoomRh");
        put("env_monitor_room", "11045", "alarmSmoke");
        put("env_monitor_room", "11288", "alarmWater");
        put("env_monitor_room", "11289", "alarmIntrusion");

        // ========== 变压器温控仪 ==========
        put("transformer_temp_controller", "10169", "transformerTempA");
        put("transformer_temp_controller", "10170", "transformerTempB");
        put("transformer_temp_controller", "10171", "transformerTempC");

        // ========== 无线测温主机 ==========
        put("wireless_temp_host", "10073", "cableTempA");
        put("wireless_temp_host", "10074", "cableTempB");
        put("wireless_temp_host", "10075", "cableTempC");

        // ========== 有源信号监测模块 / 无源信号监测模块 ==========
        // 可根据需要添加，如 signal_1, signal_2 等
    }

    /**
     * 根据设备类型和测点编码获取对应字段名
     *
     * @param deviceType 设备类型（建议传入小写）
     * @param metricCode 测点编码
     * @return 映射的字段名，不存在返回 null
     */
    public static String getFieldName(String deviceType, String metricCode) {
        if (deviceType == null || metricCode == null) return null;
        return METRIC_TO_FIELD.get(deviceType + ":" + metricCode);
    }

    private static void put(String deviceType, String metricCode, String fieldName) {
        METRIC_TO_FIELD.put(deviceType + ":" + metricCode, fieldName);
    }
}